from PyQt4.QtGui import QWidget
from PyQt4.QtGui import QPainter, QBrush
from PyQt4.QtCore import Qt, SIGNAL


import config


class BoardView(QWidget):
    def __init__(self, parent=None):
        super(BoardView, self).__init__(parent)
        self.model = None
        # size
        self.gap = 40
        self.edge = 40
        self.stoneSize = 30
        self.markSize = 10
        self.rate = 1

    def setModel(self, model):
        self.model = model

    # def mouseReleaseEvent(self, event):
    def click(self, event):
        # print('rec', event.x(), event.y())
        len = config.Len
        # If game is in progrss and click on the right Position
        # out of border
        if event.x() < self.edge or event.x() > len * self.gap + self.edge:
            return
        if event.y() < self.edge or event.y() > len * self.gap + self.edge:
            return
        if not self.model.run:
            return
        # get the pos
        xPos = -1
        yPos = -1
        for i in range(config.Len):
            lower = self.edge + (i - 0.5) * self.gap
            upper = self.edge + (i + 0.5) * self.gap
            if lower < event.x() < upper:
                xPos = i
                break
        for j in range(config.Len):
            lower = self.edge + (j - 0.5) * self.gap
            upper = self.edge + (j + 0.5) * self.gap
            if lower < event.y() < upper:
                yPos = j
                break
        # send Pos
        if self.model.get_pos(xPos, yPos) != config.ROLES.vacant:
            return
        else:
            self.emit(SIGNAL('new_step(int, int)'), xPos, yPos)
            # self.model.newStep(xPos, yPos)


    # def paintEvent(self, event):

    def paint(self, painter):
        self.drawFrame(painter)
        painter.setRenderHint(QPainter.Antialiasing, True)
        self.drawMarkPoints(painter)
        self.drawStones(painter)
        self.highlightLastStone(painter)

    def drawFrame(self, painter):
        edge = self.edge
        gap = self.gap
        for i in range(config.Len):
            painter.drawLine(edge, edge + i * gap, edge + gap * (config.Len - 1), edge + i * gap)
            painter.drawLine(edge + i * gap, edge, edge + i * gap, edge + gap * (config.Len - 1))

    def drawMarkPoints(self, painter):
        brush = QBrush()
        brush.setStyle(Qt.SolidPattern)
        brush.setColor(Qt.black)
        painter.setBrush(brush)
        # central mark point
        painter.drawEllipse(320 - 5, 320 - 5, self.markSize, self.markSize)
        # north-west mark point
        painter.drawEllipse(160 - 5, 160 - 5, self.markSize, self.markSize)
        # north-east mark point
        painter.drawEllipse(480 - 5, 160 - 5, self.markSize, self.markSize)
        # south-west mark point
        painter.drawEllipse(160 - 5, 480 - 5, self.markSize, self.markSize)
        # south - east mark point
        painter.drawEllipse(480 - 5, 480 - 5, self.markSize, self.markSize)

    def drawStones(self, painter):
        brush = QBrush()
        brush.setStyle(Qt.SolidPattern)
        edge = self.edge
        gap = self.gap
        for step in self.model.history:
            i, j, role = step
            if role == config.ROLES.firsthand:
                brush.setColor(Qt.black)
                painter.setBrush(brush)
                painter.drawEllipse(i * gap + edge - 15, j * gap + edge - 15, self. stoneSize, self.stoneSize)
            elif role == config.ROLES.secondhand:
                brush.setColor(Qt.white)
                painter.setBrush(brush)
                painter.drawEllipse(i * gap + edge - 15, j * gap + edge - 15, self. stoneSize, self.stoneSize)

    def highlightLastStone(self, painter):
        xPos, yPos, role = self.model.get_last_step()
        if (xPos is None) or (yPos is None) or (role is None):
            return
        brush = QBrush()
        brush.setStyle(Qt.SolidPattern)
        if role != config.ROLES.vacant:
            brush.setColor(Qt.red)
            painter.setBrush(brush)
            painter.drawEllipse(self.edge + xPos * self.gap - 5, self.edge + yPos * self.gap - 5, self.markSize, self.markSize)





